package com.woniu.zsd.permission.service.impl;

import com.woniu.form.AddRoleForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.permission.entity.AdminInfo;
import com.woniu.zsd.permission.entity.Roles;
import com.woniu.zsd.permission.mapper.AdminInfoMapper;
import com.woniu.zsd.permission.mapper.RolesMapper;
import com.woniu.zsd.permission.service.IRolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.zsd.permission.utils.RoleResourceCacheUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements IRolesService {

    @Autowired
    private AdminInfoMapper adminInfoMapper;

    @Autowired
    private RoleResourceCacheUtil roleResourceCacheUtil;

    @Override
    public int updateRole(Integer adminId, Integer role, Integer adminRole) {
        // 校验当前用户是否有权限（role=0 表示管理员）
        if (adminRole != 0) {
            return 0;
        }
        int i = adminInfoMapper.updateRoleByAdminId(adminId,role);
        roleResourceCacheUtil.reloadRoleResourcesCache();
        return i;
    }

    @Override
    public List<Roles> selAllRole() {
        List<Roles> roles = baseMapper.selectList(null);
        roleResourceCacheUtil.reloadRoleResourcesCache();
        return roles;
    }

    @Override
    public void insertRole(AddRoleForm arf, Integer adminId) {
        Roles roles = new Roles();
        roles.setRoleName(arf.getRoleName());
        roles.setRolesName(arf.getRolesName());
        baseMapper.insert(roles);
        roleResourceCacheUtil.reloadRoleResourcesCache();
    }
}
